package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-paths-with-the-given-xor-value/description/
 * @create 2025/8/31 13:44
 * @since JDK17
 */

public class Solution33 {
    static int N = ((int) (1e9 + 7));

    public int countPathsWithXorValue(int[][] grid, int k) {
        int n = grid.length, m = grid[0].length;
        int[][][] dp = new int[n][m][16];
        dp[0][0][grid[0][0]] = 1;
        for (int i = 1, top = grid[0][0]; i < n; i++) {
            top ^= grid[i][0];
            dp[i][0][top] = 1;
        }
        for (int i = 1, left = grid[0][0]; i < m; i++) {
            left ^= grid[0][i];
            dp[0][i][left] = 1;
        }
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                for (int l = 0; l < 16; l++) {
                    dp[i][j][l] = (dp[i][j - 1][l ^ grid[i][j]] + dp[i - 1][j][l ^ grid[i][j]]) % N;
                }
            }
        }

        return dp[n - 1][m - 1][k];
    }
}
